const jwt = require('jsonwebtoken')
const { JWT_SECRET, JWT_EXPIRE_TIME } = require('../config/config.default')

// expiresIn: '1d' 过期时间1天
const createToken = (payload, options = { expiresIn: JWT_EXPIRE_TIME }) => {
  return jwt.sign(payload, JWT_SECRET, options)
}

const verifyToken = (token) => {
  return jwt.verify(token, JWT_SECRET)
}

const jsonData = (code, message, result) => {
  return {
    code: code,
    message: message,
    result: result,
  }
}

const successData = (message = '操作成功', result = '') => {
  return jsonData(code = 200, message, result)
}

const errorData = (code = 10000, message = '操作失败，服务器异常', result = '') => {
  return jsonData(code, message, result)
}



module.exports = {
  jsonData,
  successData,
  errorData,
  createToken,
  verifyToken
}